package com.flca.frw.zk.comp;

import java.lang.reflect.Method;

import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Window;

import com.flca.frw.zk.util.Alert;
import com.flca.frw.zk.util.ListModelHelper;
import com.flca.frw.zk.util.WindowHelper;

public class FormItemComboCtrl extends Window implements IFormItem {

	private static final long serialVersionUID = -9189363508006446234L;

	private WindowHelper wh = new WindowHelper(this);
	
	public void updateView(Object value) 
	{
		Combobox cbox = (Combobox) getFellow("idInput");
		
		if (value != null) {
			ListModelHelper.setSelectedItem(cbox, value);
		} else {
			cbox.setSelectedIndex(0);
		}

	}
	
	public void updateModel(Object aTargetObject, Method setter) 
	{
		try {
			Combobox cbox = (Combobox) getFellow("idInput");
			Comboitem value = cbox.getSelectedItem();
			
			Object val = value.getValue();
			setter.invoke(aTargetObject, new Object[] {val});

		} catch (Exception e) {
			Alert.show("fout tijdens FormItemCtrl.updateModel " + e );
		} 
		
	}

	public void validate(Constraint constraint) {
		wh.validate("idInput", constraint);
	}

	public void validateNotEmpty() 
	{
		wh.validateNotEmpty("idInput");
	}

}
